# NOTES
# SAVES MEDIAN POSTERIOR MALE AND FEMALE TURNOUT

# Load package foreign 
# Load package MCMC (installed from .zip or make pkg-MCMCPack)

# Read data

data<-read.dta("jop_merge20.dta")
attach(data)
nvote<-(m+f)-v
x<-matrix(,nrow(data),2)
output<-matrix(,1,2)
XVAR<-cbind(urban, comp20, index, sumout, yss,constant)

# TRANSFORM INDEPENDENT VARIABLES FOR WEIGHTING
# KC SHOULD UPDATE MCMChierEXC to do this in C++ - not yet done

XVAR<-XVAR*sqrt(n)

# MCMChierEXC draws only from the lower diagonal of the tomography line
# Female turnout <  male turnout
# Results in the paper are burnin=2500, mcmc=7500, thin=3

posterior<-MCMChierEXC(f,m,v, nvote, XVAR, burnin=25, mcmc=75, thin=3,
 delta0=2.0, delta1=2.0, nu1=1.0, nu0=1.0, verbose=TRUE)

# SAVE POINT ESTIMATES OF MALE AND FEMALE TURNOUT

for (i in 1:nrow(data)) {
x[i,1]<-median(posterior[,i])
x[i,2]<-median(posterior[,i+nrow(data)])
}
write<-as.data.frame(x)
write.dta(write,"results_1920.dta")

# SAVE POSTERIOR

save.image("posterior_1920.RData")

detach(data)

